package com.xujianlun.cpms.service.sys.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xujianlun.cpms.common.ValidationEnum;
import com.xujianlun.cpms.entity.sys.Validation;
import com.xujianlun.cpms.mapper.sys.ValidationMapper;
import com.xujianlun.cpms.service.sys.IValidationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 邮箱验证码 服务实现类
 * </p>
 *
 * @author xujianlun
 * @since 2022-12-06
 */
@Service
public class ValidationServiceImpl extends ServiceImpl<ValidationMapper, Validation> implements IValidationService {

    @Transactional
    @Override
    public void saveCode(String email, String code, Integer type, DateTime offsetMinute) {
        Validation validation = new Validation();
        validation.setEmail(email);
        validation.setCode(code);
        validation.setType(type);
        validation.setTime(offsetMinute);
        //删除同类型的验证
        UpdateWrapper<Validation> validationUpdateWrapper = new UpdateWrapper<>();
        validationUpdateWrapper.eq("email",email);
        validationUpdateWrapper.eq("type", ValidationEnum.FORGET_PASSWORD.getCode());
        remove(validationUpdateWrapper);

        //插入新的code
        save(validation);
    }
}
